package com.abu.eduorder.service.impl;

import com.abu.eduorder.client.EduClient;
import com.abu.eduorder.client.UCenterClient;
import com.abu.eduorder.pojo.Order;
import com.abu.eduorder.mapper.OrderMapper;
import com.abu.eduorder.service.OrderService;
import com.abu.pojo.Teacher;
import com.abu.ucenter.pojo.Member;
import com.abu.utils.Resp;
import com.abu.vo.CourseWebVo;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * <p>
 * 订单 服务实现类
 * </p>
 *
 * @author 阿布
 * @since 2020-06-23
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private UCenterClient uCenterClient;
    @Autowired
    private EduClient eduClient;

    @Override
    public Order queryOrderDetails(String courseId, String memberId) {
        Order order = new Order();
//       1.根据用户id获取用户信息
        Member member = this.uCenterClient.queryMemberById(memberId);
        if (member != null) {
            order.setMemberId(memberId).setNickname(member.getNickname()).setMobile(member.getMobile());
        }
//       2.根据课程id获取课程信息
        CourseWebVo courseWebVo = this.eduClient.queryCourseWebVo(courseId);
        if (courseWebVo != null) {
//       3.根据课程获取教师id讲师名称
            String orderNo = IdWorker.getIdStr();
            order
                    .setCourseId(courseWebVo.getId())
                    .setCourseCover(courseWebVo.getCover())
                    .setTotalFee(courseWebVo.getPrice())
                    .setCourseTitle(courseWebVo.getTitle())
                    .setTeacherName(courseWebVo.getTeacherName())
//                      雪花算法生成的唯一id
                    .setOrderNo(orderNo)
                    .setPayType(2)
                    .setStatus(0);
//            插入订单信息
            this.save(order);
        }
        return order;
    }

    @Override
    public Order queryOrderInfo(String orderId) {
        return this.getOne(new QueryWrapper<Order>().eq("order_no", orderId));
    }

//    @Override
//    public Boolean queryOrderDetailsByCourseIdAndMemberId(String courseId, String memberId) {
////       1.根据用户id和课程id和状态查询订单是否已经提交
//        int count = this.count(new QueryWrapper<Order>()
//                .eq("course_id", courseId)
//                .eq("member_id", memberId)
//                .eq("status", 1));
//        if (count != 0) {
//            return true;
//        }
//        return false;
//    }
}
